<template>
  <div class="chatPanel">
    <ul>
      <li class="messsagecontainer">
        <dl>
          <dd
            class="frommsg"
            :style="{textAlign: el.type== 'to' ? 'right' : 'left', color: el.type== 'to' ? 'red' : 'green'}"
            v-for="(el, index) in frommsg"
          >{{el.text}}</dd>
        </dl>
      </li>
      <li class="sender">
        <div class="text">
          <van-cell-group>
            <van-field v-model="sendvalue" placeholder="发送消息" bind:change="onChange" />
          </van-cell-group>
        </div>
        <div class="sendbnt">
          <van-button style="margin-left:5px" @click="bnt" type="info">发送消息</van-button>
        </div>
      </li>
    </ul>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from "vue-property-decorator";
import eventBus from "../../server/eventBus";
import webrtc from "../../server/webrtc";
@Component
export default class HelloWorld extends Vue {
  @Prop() private msg!: string;
  private myList: any = [];
  private chatList: any = [];
  private frommsg: any = [];
  private tomsg: any = [];
  private value: string = "";
  private sendvalue: string = "";
  public onChange(val: any) {
    console.log("onchange:", val);
  }

  public recvRtcMsg(msg: any) {
    this.frommsg.push({
      type: "from",
      text: msg,
    });
  }

  public bnt() {
    console.log("this.sendvalue 发送消息:", this.sendvalue);
    if (!this.sendvalue) {
      return;
    }

    if (webrtc.sendChannel) {
      this.frommsg.push({
        type: "to",
        text: this.sendvalue,
      });
      webrtc.sendChannel.send(this.sendvalue);
    }
    this.sendvalue = "";
  }

  public mounted() {
    eventBus.$on("recvRtcMsg", this.recvRtcMsg);
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.chatPanel {
  .messsagecontainer {
    height: 450px;
    background: #fbfbfb;
    overflow-y: auto;
    .frommsg {
      text-align: right;
      color: red;
      min-height: 40px;
      line-height: 30px;
      padding: 10px;
    }
    .tomsg {
      text-align: right;
    }
  }
  .sender {
    width: 100%;
    position: absolute;
    background: #fbfbfb;
    display: flex;
    bottom: 0px;
    height: 48px;
  }
}
</style>
